﻿@using eLoan.Models;
@model DailyDuePaymentModel

@{
    ViewBag.Title = "Daily Due Payments";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Daily Due Payments</h2>
        <ol class="breadcrumb">
            <li>
                Report
            </li>
            <li class="active">
                <strong>Daily Due Payments</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("ExportDailyDuePayments", "Report", FormMethod.Post, new { id = "saveForm" }))
            {
    <fieldset>
        <div class="ibox-title">
            <div class="divSearch">
                <table class="table-search">
                    <tr>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.Branches)</span>
                        </th>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.Agents)</span>
                        </th>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.FromDate)</span>
                        </th>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.ToDate)</span>
                        </th>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.SearchText)</span>
                        </th>
                        <th></th>
                    </tr>
                    <tr>
                        <td>
                            @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line", @width = "50%", @onchange = "GetAgents();" })
                        </td>
                        <td style="padding-left:10px">
                            @Html.DropDownListFor(m => m.SelectedAgentID, Model.Agents, new { @id = "ddlAgents", @class = "text-box single-line", @width = "50%", @onchange = "Refresh()" })
                        </td>
                        <td style="padding-left:10px">
                            @Html.TextBoxFor(m => m.FromDate, "{0:MM/dd/yyyy}", new { @class = "form-control input datepicker" })
                        </td>
                        <td style="padding-left:10px">
                            @Html.TextBoxFor(m => m.ToDate, "{0:MM/dd/yyyy}", new { @class = "form-control input datepicker" })
                        </td>
                        <td style="padding-left:10px">
                            @Html.TextBoxFor(m => m.SearchText, new { @id = "txtSearchText", @class = "single-line", @width = "50%" })
                        </td>
                        <td style="vertical-align:bottom">
                            <button type="submit" id="btnSearch" class="btn btn-primary btnSearch" name="command" value="Refresh">Search</button>
                        </td>
                        <td>
                            <button type="submit" id="btnSave" class="btn btn-primary" name="command" value="SaveDaily">Save</button>
                        </td>
                        <td>
                            <button type="submit" id="btnExport" class="btn btn-primary" name="command" value="ExportDaily">Export to excel</button>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="ibox-content">
            <table id="tbl" class="table table-bordered table-List">
                <thead>
                    <tr>
                        <th>Document No</th>
                        <th>Borrower</th>
                        <th>Payment Frequency</th>
                        <th>Due Date</th>
                        <th>Due Amount</th>
                        <th>Amount Paid</th>
                        <th>Payment Type</th>
                        <th>Cheque Details</th>
                        <th>Contact Numbers</th>
                        <th>Rermarks</th>
                    </tr>
                </thead>
                <tbody id="tableBody">
                    @for (var i = 0; i < Model.DailyDuePaymentItems.Count(); i++)
                    {
                    <tr class="trData">
                        <td>
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].DocumentNo)
                            @Html.DisplayFor(m => m.DailyDuePaymentItems[i].DocumentNo)
                        </td>
                        <td>
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].BorrowerName)
                            @Html.DisplayFor(m => m.DailyDuePaymentItems[i].BorrowerName)
                        </td>
                        <td>
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].PaymentFrequency)
                            @Html.DisplayFor(m => m.DailyDuePaymentItems[i].PaymentFrequency)
                        </td>
                        <td>
                            @Html.ValueFor(m => m.DailyDuePaymentItems[i].DueDate, "{0:MM/dd/yyyy}")
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].DueDate)
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].ScheduleID)
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].PaymentID)
                        </td>
                        <td style="text-align:right">
                            @Html.ValueFor(m => m.DailyDuePaymentItems[i].DueAmount, "{0:#,###,##0.00}")
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].DueAmount)
                        </td>
                        <td style="text-align:right">
                            @Html.ValueFor(m => m.DailyDuePaymentItems[i].AmountPaid, "{0:#,###,##0.00}")
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].AmountPaid)
                        </td>
                        <td>
                            @Html.ValueFor(m => m.DailyDuePaymentItems[i].PaymentTypeName)
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].PaymentTypeName)
                        </td>
                        <td>
                            @Html.ValueFor(m => m.DailyDuePaymentItems[i].ChequeDetails)
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].ChequeDetails)
                        </td>
                        <td>
                            @Html.ValueFor(m => m.DailyDuePaymentItems[i].ContactNumbers)
                            @Html.HiddenFor(m => m.DailyDuePaymentItems[i].ContactNumbers)
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.DailyDuePaymentItems[i].DueRemarks, new { @class = "single-line" })
                        </td>
                    </tr>
                    }
                </tbody>
            </table>

            <button type="submit" id="btnRefresh" style="display:none" name="command" value="Refresh">Refresh</button>

        </div>
    </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
@Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
@Styles.Render("~/plugins/dataPickerStyles")
    <style>

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search th {
            width: 250px;
        }

            .table-search th label {
                margin-top: 15px;
                margin-left: 20px;
            }

            .table-search th .span {
                padding-left: 10px;
            }

        .table-search .align-right{
            text-align: right;
        }

            .table-search td select {
                margin-left: 15px !important;
                margin-bottom: 10px !important;
            }

            .table-search td input {
                margin-left: 25px !important;
                margin-bottom: 10px !important;
            }

            .table-search td button {
                margin-right: 15px;
                margin-bottom: 10px !important;
            }

        .table-search td .btnSearch {
            margin-bottom: 0px;
            margin-left: 40px !important;
        }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
    }

</style>

}

@section Scripts {
@Scripts.Render("~/plugins/dataTables")
@Scripts.Render("~/plugins/dataPicker")

<script type="text/javascript">

    $(document).ready(function () {

        $('.table-List').dataTable({
            ordering: true,
            searching: false,
            paging: false,
            scrollY: 400,
            scrollX: true
        });
        
        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                Refresh();
                return false;
            }
        });

        $('.datepicker').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            format: 'mm/dd/yyyy',
            autoclose: true

        });

        SetDateFormat();
    });

    function Refresh() {
        document.getElementById('btnRefresh').click();
    }

    function GetTotal()
    {
        var totalAmount = 0;
        $('.amount').each(function () {
            var val = $(this).val().replace(',','');
            
            totalAmount = parseFloat(totalAmount) + parseFloat(val);

        });

        var total = $('#total');
        total.text(FormatNumber(totalAmount.toFixed(2)));
    }

    function FormatNumber(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function FormatMe(me) {
        var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

        if (isNaN(parseFloat(val)))
            val = 0.00;

        var decimalFormat = parseFloat(val).toFixed(2);
        var commaFormat = FormatNumber(decimalFormat);

        $(me).val(commaFormat);
    }

    function GetAgents()
    {
        var val = $('#ddlBranches').val();
        var subItems = "";

        $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
    function (data) {

        subItems += "<option value='0'>All</option>"
        $.each(data, function (index, item) {
            subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"
        });

        $("#ddlAgents").html(subItems)
    });
    }

    function SetDateFormat() {

        $('#pd .input-group.date').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            format: 'mm/dd/yyyy',
            autoclose: true

        });

        var pd = $('#txtDueDate').val();
        $('#pd .input-group.date').datepicker("update", new Date(pd));

    }

</script>

}
